import { reqGetDetailInfoData } from '@/api';

const state = {
  detailInfo: {}
};
const actions = {
  // 1. 获取商品详情数据
  async getDetailInfoData({ commit }, skuId) {
    const res = await reqGetDetailInfoData(skuId);
    if (res.code === 200) {
      commit('SAVE_DETAIL_INFO_DATA', res.data);
    } else {
      console.log(res.message);
    }
  }
};
const mutations = {
  // 1. 存储获取来的详情对象数据
  SAVE_DETAIL_INFO_DATA(state, detailInfo) {
    state.detailInfo = detailInfo;
  },
  // 2. 更新大图链接
  UPDATE_SKUINFO_DEFAULT_IMGURL(state,skuImg) {
    // 2.1 循环遍历图片列表，先将所有的isDefault设置为0
    state.detailInfo.skuInfo.skuImageList.forEach(item => {
      item.isDefault = '0'
    })
    // 2.2 当前被单击的小图片要设置成高亮
    skuImg.isDefault = '1'
    // 2.3 实现小图切换大图 也就是将被单击的小图的链接给到大图链接 
    state.detailInfo.skuInfo.skuDefaultImg = skuImg.imgUrl
  }
};
const getters = {
  categoryView(state) {
    return state.detailInfo.categoryView || {};
  },
  skuInfo(state) {
    return state.detailInfo.skuInfo || {};
  },
  spuSaleAttrList(state) {
    return state.detailInfo.spuSaleAttrList || [];
  }
};
export default {
  namespaced: true, // 开启命名空间 用于区分不同的模块
  state,
  actions,
  mutations,
  getters
};
